package com.gotop.system.service.impl.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gotop.common.constant.DefaultConstant;
import com.gotop.common.tenant.TenantId;
import com.gotop.common.utils.DigestUtil;
import com.gotop.common.utils.R;
import com.gotop.entity.system.Role;
import com.gotop.entity.system.Tenant;
import com.gotop.entity.user.User;
import com.gotop.mapper.system.RoleDao;
import com.gotop.mapper.system.TenantDao;
import com.gotop.mapper.user.UserDao;
import com.gotop.system.service.system.ITenantService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: cem
 * @description:
 * @author: superfish
 * @create: 2020-12-25 11:22
 **/
@Service
@AllArgsConstructor
public class TenantServiceImpl extends ServiceImpl<TenantDao, Tenant> implements ITenantService {

    @Autowired
    private TenantId tenantId;
    @Autowired
    private RoleDao roleMapper;
//    private final DeptMapper deptMapper;
//    private final IPostService postService;
    @Autowired
    private UserDao userDao;


    @Override
    public IPage<Tenant> selectTenantPage(IPage<Tenant> page, Tenant tenant) {
        return page.setRecords(baseMapper.selectTenantPage(page, tenant));
    }

    @Override
    public Tenant getByTenantId(String tenantId) {
        return getOne(Wrappers.<Tenant>query().lambda().eq(Tenant::getTenantId, tenantId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveTenant(Tenant tenant) {
        if (null != tenant.getId()) {
            List<Tenant> tenants = baseMapper.selectList(Wrappers.<Tenant>query().lambda().eq(Tenant::getIsDeleted, DefaultConstant.DB_NOT_DELETED));
            List<String> codes = tenants.stream().map(Tenant::getTenantId).collect(Collectors.toList());
            String tenantId = getTenantId(codes);
            tenant.setTenantId(tenantId);
            // 新建租户对应的默认角色
            Role role = new Role();
            role.setTenantId(tenantId);
            role.setParentId(0L);
            role.setRoleName("管理员");
            role.setRoleAlias("admin");
            role.setSort(2);
            role.setIsDeleted(0);
            roleMapper.insert(role);
            // 新建租户对应的默认部门
//            Dept dept = new Dept();
//            dept.setTenantId(tenantId);
//            dept.setParentId(0L);
//            dept.setDeptName(tenant.getTenantName());
//            dept.setFullName(tenant.getTenantName());
//            dept.setSort(2);
//            dept.setIsDeleted(0);
//            deptMapper.insert(dept);
//            // 新建租户对应的默认岗位
//            Post post = new Post();
//            post.setTenantId(tenantId);
//            post.setCategory(1);
//            post.setPostCode("ceo");
//            post.setPostName("首席执行官");
//            post.setSort(1);
//            postService.save(post);
            // 新建租户对应的默认管理用户
            User user = new User();
            user.setTenantId(tenantId);
            user.setName("admin");
            user.setRealName("admin");
            user.setAccount("admin");
            user.setPassword(DigestUtil.encrypt("admin"));
            user.setRoleId(String.valueOf(role.getId()));
//            user.setDeptId(String.valueOf(dept.getId()));
//            user.setPostId(String.valueOf(post.getId()));
            user.setBirthday(new Date());
            user.setSex(1);
            user.setIsDeleted(DefaultConstant.DB_NOT_DELETED);
            userDao.insert(user);
        }
        return super.saveOrUpdate(tenant);
    }

    private String getTenantId(List<String> codes) {
        String code = tenantId.generate();
        if (codes.contains(code)) {
            return getTenantId(codes);
        }
        return code;
    }

    @Override
    public boolean deleteLogic(List<Long> ids) {
//        userDao.removeByIds(ids);
        return false;
    }
}